package UML_ER.diagram.part;



import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;





import UML_ER.Attribute;
import UML_ER.AttributeType;
import UML_ER.diagram.edit.commands.ApplyUnapplyAttributeTypeCommand;
import UML_ER.diagram.edit.parts.AttributeEditPart;

public class AttributeTypeAction implements IObjectActionDelegate {

	private Shell shell;
	private AttributeEditPart selectedElement;
	
	/**
	 * Constructor for Action1.
	 */
	public AttributeTypeAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		String menu = action.getId();
		ApplyUnapplyAttributeTypeCommand.ApplyUnapplyAttributeTypeRequest request = null;
		if(menu.equals("UML_ER_tool.ApplyPrimaryKey")){
			 request = new ApplyUnapplyAttributeTypeCommand.ApplyUnapplyAttributeTypeRequest((Attribute)((View)selectedElement.getModel()).getElement(), AttributeType.PRIMARY_KEY);
		}
		if(menu.equals("UML_ER_tool.ApplyDerivedAttribute")){
			request = new ApplyUnapplyAttributeTypeCommand.ApplyUnapplyAttributeTypeRequest((Attribute)((View)selectedElement.getModel()).getElement(), AttributeType.DERIVED);
		}
		if(menu.equals("UML_ER_tool.ApplyMultiValuedAttribute")){
			request = new ApplyUnapplyAttributeTypeCommand.ApplyUnapplyAttributeTypeRequest((Attribute)((View)selectedElement.getModel()).getElement(), AttributeType.MULTI_VALUED);
		}
		if(menu.equals("UML_ER_tool.UnapplyAttributeType")){
			request = new ApplyUnapplyAttributeTypeCommand.ApplyUnapplyAttributeTypeRequest((Attribute)((View)selectedElement.getModel()).getElement(), AttributeType.STANDARD);
		}
		
		
		Command command = new ICommandProxy(new ApplyUnapplyAttributeTypeCommand(request));
		
		selectedElement.getDiagramEditDomain().getDiagramCommandStack().execute(command);
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		selectedElement = null;
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			if (structuredSelection.getFirstElement() instanceof AttributeEditPart) {
				selectedElement = (AttributeEditPart) structuredSelection
						.getFirstElement();
				System.out.println("HELP");
			}
			
		}
	}

}
